<template>
	<div style="width:100%;height: 100%;position: absolute;">
		<!--div头部-->
		<div style="position: absolute;top: 0;left: 0;width: 100%">
			<div style="position: relative;z-index: 500">
				<my-header :title="'食材检测报告'" :hasTitle="true" :hasBack="true"></my-header>
			</div>
			<!--<tab-head if="headList" :titleInfor="headList" :pullDownMemu="false" :width="'6rem'">

			</tab-head>-->
			<tab-head class="cjy-slider" :titleInfor="headList">
				<div slot="textBox" slot-scope="props" :class="{active :props.isActive}" class="cjy-test-header">

					<span> {{props.title}}</span>
				</div>
			</tab-head>
		</div>
		<div class="centent" style="width: 100%;height:100%; padding-top:5.2rem">
			<!--商品列表-->

			<div style="width:100%; height:100%">
				<Translate-main :lengths="headList.length">
					<div class="aui-content">
						<ul class="aui-list aui-media-list commodity_list subtotal_list">
							<li class="aui-list-item aui-list-item-arrow baoguo-list-item baoguo-root" v-for="(item,index) in contentDate" @click="toDetail(item)" :key="index">
								<div class="aui-media-list-item-inner">
									<div class="aui-list-item-media" style="width:4.3125rem;height:4.3125rem ">
										<img :src="item.inspectImg?item.inspectImg:'static/image/demo1.png'" alt="" />
									</div>
									<div class="aui-list-item-inner suggest" style="text-align: left;height: 1.9rem;">
										<h3 class="time">{{myGetTime(item.inspectTm)}}&nbsp;&nbsp;{{item.inspectTitle}}</h3>
										<h3 class="jieguo">{{item.inspectResult}}</h3>

									</div>
								</div>
							</li>
						</ul>
					</div>
				</translate-main>
			</div>
			<div id="contorlShow" v-if="contentDate.length == 0">
				<div id="disShow">
					<img src="static/image/no_news.png" alt="" />
					<p>暂无检测数据</p>
				</div>
			</div>
			<!--报告列表 end-->
		</div>
	</div>

</template>

<script>
	import { mapGetters, mapMutations } from "vuex";
	import TranslateMain from "../../real-components/TranslateMain/index1";
	//	import TabHead from "../../real-components/TabHead";
	import TabHead from '@/real-components/TabHead/edit.vue'
	import Header from "../header";
	import axios from "axios";
	import { MessageBox } from '@/ui-components';
	export default {
		name: 'hello',
		data() {
			return {

				//导航列表
				headList: [],
				contentDate: [],
				innitDateOne: null
			}
		},
		computed: {

			num() {
				return this.$store.state.cjy.classifyIndex
			}
		},
		watch: {
			num(newdd, olddd) {
				let str = this.headList[this.num].goodsClassName;
				let num = str.indexOf("年");
				let month = str[0];
				let time = '';
				if(num == -1) {
					time = this.getDateParam(str, true); //当前年
				} else {
					time = this.getDateParam(str, false); //往年
				}
				console.log(time);
				let params = {
					inspectionDate: time,
					storeCode: window.localStorage.getItem("storeCode")
				};
				this.initData(params);
			}
		},
		components: {
			"TabHead": TabHead,
			"TranslateMain": TranslateMain,
			"my-header": Header
		},
		methods: {
			goBack() {
				this.$router.push("profile");
			},
			//匹配字符串  获取请求接口的参数
			getDateParam(str, flag) {
				let arr = ["一", "二", "三", "四", "五", "六", "七", "八", "九", "十"];
				if(flag) {
					let year = new Date().getFullYear();
					let mystr = '';
					for(let i = 0; i < arr.length; i++) {

						if(arr[i] == str.substr(0, 1)) {
							mystr += (i + 1);
						}
						if(arr[i] == str.substr(1, 1)) {
							mystr += (i + 1);
						}
					}
					//					console.log(mystr,'殺殺殺')
					if(mystr.length > 2) {
						return year + "-" + mystr.substr(1, 1) + mystr.substr(0, 1);
					} else {
						if(mystr == '10') {
							return year + "-" + mystr.substr(0, 1) + mystr.substr(1, 1);
						} else {
							return year + "-" + '0' + mystr;
						}
					}
				} else {

					let mystr = str.substr(0, 4);
					mystr += '-'
					for(let i = 0; i < arr.length; i++) {
						if(arr[i] == str.substr(5, 1)) {
							mystr += (i + 1);
						}
						if(arr[i] == str.substr(6, 1)) {
							mystr += (i + 1);
						}
					}
					if(mystr.length > 6) {
						if(mystr.substr(5, 2) == '10') {
							return mystr.substr(0, 5) + mystr.substr(5, 1) + mystr.substr(6, 1);
						} else {
							return mystr.substr(0, 5) + mystr.substr(6, 1) + mystr.substr(5, 1);
						}

					} else {
						return mystr.substr(0, 4) + mystr.substr(4, 1) + '0' + mystr.substr(5, 1);
					}
				}

			},
			//转换时间格式
			myGetTime(param) {
				console.log("进来转换时间")
				if(param) {
					let date = new Date(param);
					let year = date.getFullYear();
					let month = checkTime(date.getMonth() + 1);
					let day = checkTime(date.getDate());
					let hour = checkTime(date.getHours());
					let min = checkTime(date.getMinutes());
					let sec = checkTime(date.getSeconds());

					function checkTime(i) {
						if(i < 10) {
							i = '0' + i;
						}
						return i;
					}

					return month + '月' + day + '号';
				} else {
					return '';
				}
			},
			toDetail(item) {
				this.$router.push({
					path: 'foodCheckDetail',
					query: {
						item: item,
						num: this.num
					}
				});
			},
			//	    	保持回复原样
			noChange() {
				let query = this.$route.query.num;
				if(query) {
					this.$store.state.cjy.classifyIndex = query;
				} else {
					this.$store.state.cjy.classifyIndex = 0;
				}
			},
			getMonth(start, end) {
				var result = [];
				var starts = start.split('-');
				var ends = end.split('-');
				var staYear = parseInt(starts[0]);
				var staMon = parseInt(starts[1]);
				var endYear = parseInt(ends[0]);
				var endMon = parseInt(ends[1]);
				while(staYear <= endYear) {
					if(staYear === endYear) {
						staYear++;
						while((staMon - 1) < endMon) {

							let obj = {};
							//							console.log(this.getTime(staMon),'月份')
							obj.goodsClassNum = '';
							obj.goodsClassName = (this.getTime(staMon) + '月份');
							//							console.log(obj);
							result.push(obj);
							staMon++;
						}

					} else {

						staMon++;
						if(staMon > 12) {
							staMon = 1;
							staYear++;
						}
						let obj = {};
						obj.goodsClassNum = '';
						obj.goodsClassName = staYear + '年' + (this.getTime(staMon) + '月份');
						result.push(obj);
					}
				}

				this.headList = result.reverse();
				var str = new Date().getFullYear() + '年' + '一月份';

				//当前年多一个月 干脆移除该元素
				for(let i = 0; i < this.headList.length; i++) {
					if(this.headList[i].goodsClassName === str) {
						this.headList.splice(i, 1);
					}
				}
			},
			getTime(n) {
				switch(n) {
					case 1:
						return '一'
						break;
					case 2:
						return '二'
						break;
					case 3:
						return '三'
						break;
					case 4:
						return '四'
						break;
					case 5:
						return '五'
						break;
					case 6:
						return '六'
						break;
					case 7:
						return '七'
						break;
					case 8:
						return '八'
						break;
					case 9:
						return '九'
						break;
					case 10:
						return '十'
						break;
					case 11:
						return '十一'
						break;
					case 12:
						return '十二'
						break;
					default:
						return "一"
						break;
				}
			},
			initData(params) {
				let url = window.api.base_host + "/inspectionReport";
				console.log()
				let config = {
					url: url,
					method: "GET",
					params,
					headers: {
						"sessionId": localStorage.getItem('sessionId')
					}
				}
				console.log(config, '111111111111111111111')
				axios(config).then((res) => {
					//					console.log(res.data.returnCode,'请求的uju');
					//					console.log(res.data.data,'请求的uju');
					if(res.data.returnCode == 1) {
						var arr = res.data.data[0].results;

						var compare = function(prop) {
							return function(obj1, obj2) {
								var val1 = obj1[prop];
								var val2 = obj2[prop];
								if(val1 < val2) {
									return -1;
								} else if(val1 > val2) {
									return 1;
								} else {
									return 0;
								}
							}
						}
						this.contentDate = arr.sort(compare("inspectTm")).reverse();
						console.log(this.contentDate, '请求的数据');
					}
				})
			},
			getInitParams() {
				let mydate = new Date();
				let time = mydate.getFullYear() + '-' + (mydate.getMonth() + 1);
				let params = {
					inspectionDate: time,
					storeCode: window.localStorage.getItem("storeCode")
				};
				return params;
			},
			getMyMonthDate() {
				let mydate = new Date();
				this.innitDateOne = mydate.getFullYear() + '-' + (mydate.getMonth() + 1) + '-' + mydate.getDay();
			}

		},
		created() {
			if(!localStorage.getItem('sessionId')) {
				MessageBox('用户未登录账号', '去登陆').then(
					(res) => {
						this.$router.push({
							path: 'login',
							query: {
								page: 'FoodChecked'
							}
						})
					}
				)
			}
			this.getMyMonthDate();
			this.noChange(); //导航条样式激活位置
			this.initData(this.getInitParams()); //初始化数据
			this.getMonth("2016-6-6", this.innitDateOne);
		},
		mounted() {
			// initDomTree(this.getDomTree[this.page]);

		},
		updated() {
			//页面更新完毕
			// initDomTree(this.getDomTree[this.page]);
		}
	}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
	.aui-searchbar,
	.aui-bar-nav {
		position: static;
		left: 0;
		top: 0;
		z-index: 12;
	}
	
	.subtotal_list .aui-list-item-media {
		width: 22vw;
		height: 22vw;
	}
	
	.commodity_list .suggest h3 {
		font-size: 0.8rem;
		min-height: 1.5rem;
	}
	
	.jieguo {
		font-weight: bolder;
		color: #FF5521;
	}
	
	.time {
		margin-top: 0.9rem;
	}
	
	.cjy-tab-item[data-v-28c4c2e6] {
		width: 6rem !important;
	}
	
	#disShow {
		position: absolute;
		padding: 3rem;
		top: 5.5rem;
		text-align: center;
	}
	
	.cjy-test-header {
		line-height: 2.2rem;
		height: 2.2rem;
		width: 5.5rem;
	}
	
	.cjy-slider {
		height: 2.2rem;
	}
</style>